package com.jn.erp.wechat.common.api.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jn.erp.wechat.common.api.entity.WxMenu;
import com.jn.erp.wechat.common.api.mapper.WxMenuMapper;
import com.jn.erp.wechat.common.api.service.WxMenuService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <pre>
 *
 * </pre>
 *
 * @author sunjiyong
 * @since 2024/9/10 19:58
 */
@Service
public class WxMenuServiceImpl extends ServiceImpl<WxMenuMapper, WxMenu> implements WxMenuService {

    @Override
    public List<WxMenu> getTreeByRouteName(WxMenu wxMenu) {
        QueryWrapper<WxMenu> qw=new QueryWrapper<>();
        qw.eq("status",0);
        qw.eq("menu_type","E");
        List<WxMenu> wxMenus = baseMapper.selectList(qw);
        List<WxMenu> filter = wxMenus.stream().filter(m -> StrUtil.equals(m.getRouteName(), wxMenu.getRouteName())).collect(Collectors.toList());
        if(filter.size()!=1){
            return null;
        }
        WxMenu mparent = filter.get(0);
        List<WxMenu> childPerms = getChildPerms(wxMenus, mparent.getMenuId());
        return childPerms;
    }


    /**
     * 根据父节点的ID获取所有子节点
     *
     * @param list     分类表
     * @param parentId 传入的父节点ID
     * @return String
     */
    public List<WxMenu> getChildPerms(List<WxMenu> list, Long parentId) {
        List<WxMenu> returnList = new ArrayList<WxMenu>();
        for (Iterator<WxMenu> iterator = list.iterator(); iterator.hasNext(); ) {
            WxMenu t = (WxMenu) iterator.next();
            // 一、根据传入的某个父节点ID,遍历该父节点的所有子节点
            if (t.getParentId() == parentId) {
                recursionFn(list, t);
                returnList.add(t);
            }
        }
        return returnList;
    }

    @Override
    public List<WxMenu> wxRoleMenus(String roles) {
        return baseMapper.wxRoleMenus(roles);
    }

    @Override
    public List<WxMenu> wxUserRoleMenus(WxMenu wxMenu) {
        return baseMapper.wxUserRoleMenus(wxMenu);
    }

    @Override
    public List<WxMenu> wxBarMenus(String roles) {
        return baseMapper.wxBarMenus(roles);
    }

    /**
     * 递归列表
     *
     * @param list 分类表
     * @param t    子节点
     */
    private void recursionFn(List<WxMenu> list, WxMenu t) {
        // 得到子节点列表
        List<WxMenu> childList = getChildList(list, t);
        t.setChildren(childList);
        for (WxMenu tChild : childList) {
            if (hasChild(list, tChild)) {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<WxMenu> getChildList(List<WxMenu> list, WxMenu t) {
        List<WxMenu> tlist = new ArrayList<WxMenu>();
        Iterator<WxMenu> it = list.iterator();
        while (it.hasNext()) {
            WxMenu n = (WxMenu) it.next();
            if (n.getParentId().longValue() == t.getMenuId().longValue()) {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<WxMenu> list, WxMenu t) {
        return getChildList(list, t).size() > 0;
    }

}
